package com.bdq.content.infrastructure.persistence.mysql.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdq.common.enums.InteractionTypeEnum;
import com.bdq.common.enums.YesOrNoEnum;
import com.bdq.content.infrastructure.model.dto.ContentQueryInfraDTO;
import com.bdq.content.infrastructure.persistence.mysql.pojo.Content;
import com.bdq.content.infrastructure.persistence.mysql.service.ContentService;
import com.bdq.content.infrastructure.persistence.mysql.mapper.ContentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
* @author malei
* @description 针对表【content(内容表)】的数据库操作Service实现
* @createDate 2024-05-10 17:16:38
*/
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content>
    implements ContentService{

    @Resource
    private ContentMapper contentMapper;

    @Override
    public IPage<Content> contentSearch(String keyword, Integer pageNo, Integer pageSize) {
        Page<Content> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);

        return lambdaQuery()
                .apply("MATCH(text) AGAINST ({0} IN NATURAL LANGUAGE MODE)", keyword)
                .eq(Content::getIsDeleted, YesOrNoEnum.NO.getCode())
                .orderByDesc(Content::getId)
                .page(page);
    }

    @Override
    public IPage<Content> contentQuery(ContentQueryInfraDTO infraDTO) {
        Page<Content> page = new Page<>();
        page.setCurrent(infraDTO.getPageNo());
        page.setSize(infraDTO.getPageSize());
        if (infraDTO.getChannel() == 0) {
            return contentMapper.recommendQuery(page, infraDTO);
        }else {
            return contentMapper.contentQuery(page, infraDTO);
        }
    }

    @Override
    public IPage<Content> pageUserPost(Long userId, Integer channel, Integer pageNo, Integer pageSize) {
        Page<Content> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);

        return lambdaQuery()
                .eq(Content::getUserId, userId)
                .eq(Objects.nonNull(channel), Content::getChannel, channel)
                .eq(Content::getIsDeleted, YesOrNoEnum.NO.getCode())
                .orderByDesc(Content::getId)
                .page(page);
    }
}




